﻿using UnityEngine;
using System.Collections;

public class ProgressBar : MonoBehaviour {

	public Texture2D emptyProgressBar;
	public Texture2D fullProgressBar;
	public float progressSpeed = 0.03f;
	
	Vector2 size = new Vector2(70, 10);

	
	float progress;
	bool activateProgress;

	// Use this for initialization
	void Start () {

	}
	
	// Update is called once per frame
	void Update () {
		if (activateProgress) {
			progress += Time.deltaTime * progressSpeed;
		}

	}

	public void SetActivateProgress(bool activate) {
		activateProgress = activate;
		if (!activate) {
			progress = 0;
		}
	}

	public bool ProgressComplete() {
		return progress >= 1;
	}

	void OnGUI() {
		if (activateProgress) {
			Vector3 pos = Camera.main.WorldToScreenPoint(this.transform.position);
			pos.y = (Screen.height - pos.y);
			pos.x -= size.x/2;

			GUI.DrawTexture(new Rect(pos.x, pos.y, size.x, size.y), emptyProgressBar);
			GUI.BeginGroup(new Rect (pos.x, pos.y, size.x * Mathf.Clamp01(progress), size.y));
			GUI.DrawTexture(new Rect(0, 0, size.x, size.y), fullProgressBar);
			GUI.EndGroup();
		}
	}
}
